home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’95 / NetFractal™ / Fractal 8 source / TriPatch.c < prev   
Encoding:
C/C++ Source or Header  |  1995-06-24  |  5.3 KB  |  214 lines  |  [TEXT/MPS ]

  1. /*    
  2.     File:        TriPatch.c
  3.  
  4.     Used to build:    “Fractal 7”
  5.     
  6.     Written by:        Dan Clifford        November 1994
  7.  
  8.     Description:
  9.         The following code implements a patch-shading algorithm
  10.         for doing surface shading.
  11. */
  12.  
  13.  
  14. #include "Fractal.h"
  15.  
  16. unsigned char *gBlitData = NULL;
  17. Rect gBlitBounds = { 0, 0, 0, 0 };
  18. short gBlitRowBytes = 0;
  19.  
  20.  
  21. /*
  22.     SetBlitBuffer
  23.     
  24.     Call this function to specify the blitting buffer as well as the 
  25.     bounds and the number of bytes per row.
  26. */
  27. void SetBlitBuffer(unsigned char *blitData, Rect *blitBounds, short blitRowBytes)
  28. {
  29.     gBlitData = blitData;
  30.     gBlitRowBytes = blitRowBytes;
  31.     gBlitBounds = *blitBounds;
  32. }
  33.  
  34.  
  35. /*
  36.     FillTriangle
  37.     
  38.     This function fills in a triangular patch of pixels. It assumes the offscreen
  39.     buffer is an 8-byte buffer, and fills in each pixel with the specified color.
  40. */
  41. void FillTriangle(long vertex1H, long vertex1V, long vertex2H, long vertex2V, 
  42.         long vertex3H, long vertex3V, unsigned char color)
  43. {
  44.     if (vertex2V < vertex1V) {
  45.         long    temp;
  46.  
  47.         temp = vertex2V;
  48.         vertex2V = vertex1V;
  49.         vertex1V = temp;
  50.  
  51.         temp = vertex2H;
  52.         vertex2H = vertex1H;
  53.         vertex1H = temp;
  54.     }
  55.     
  56.     if (vertex3V < vertex1V) {
  57.         long    temp;
  58.  
  59.         temp = vertex3V;
  60.         vertex3V = vertex1V;
  61.         vertex1V = temp;
  62.  
  63.         temp = vertex3H;
  64.         vertex3H = vertex1H;
  65.         vertex1H = temp;
  66.     }
  67.     
  68.     if (vertex3H < vertex2H) {
  69.         long    temp;
  70.  
  71.         temp = vertex3V;
  72.         vertex3V = vertex2V;
  73.         vertex2V = temp;
  74.  
  75.         temp = vertex3H;
  76.         vertex3H = vertex2H;
  77.         vertex2H = temp;
  78.     }
  79.  
  80.     if (vertex3V > vertex2V) {
  81.         long            currentLeftH,
  82.                         currentRightH,
  83.                         currentLeftIncrement,
  84.                         currentRightIncrement;
  85.         long            currentV; 
  86.         unsigned char    *currentPixel;
  87.                 
  88.         currentLeftH = (long)(vertex1H) << 16;
  89.         currentRightH = currentLeftH;
  90.  
  91.         currentLeftIncrement = ((long)(vertex2H - vertex1H) << 16) / (vertex2V - vertex1V);
  92.         currentRightIncrement = ((long)(vertex3H - vertex1H) << 16) / (vertex3V - vertex1V);
  93.             
  94.         for (currentV = vertex1V; currentV < vertex2V; currentV++) {
  95.  
  96.             long    currentRightHlong = currentRightH >> 16;
  97.             long    currentLeftHlong = currentLeftH >> 16;
  98.             long    currentH;
  99.             
  100.             currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
  101.  
  102.             for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
  103.                 *currentPixel = color;
  104.                 currentPixel++;
  105.             }
  106.             
  107.             currentLeftH += currentLeftIncrement;
  108.             currentRightH += currentRightIncrement;
  109.         
  110.         }
  111.  
  112.         currentLeftH = (long)(vertex2H) << 16;
  113.         currentLeftIncrement = ((long)(vertex3H - vertex2H) << 16) / (vertex3V - vertex2V);
  114.  
  115.         for (currentV = vertex2V; currentV <= vertex3V; currentV++) {
  116.             long    currentRightHlong = currentRightH >> 16;
  117.             long    currentLeftHlong = currentLeftH >> 16;
  118.             long    currentH;
  119.             
  120.             currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
  121.  
  122.             for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
  123.                 *currentPixel = color;
  124.                 currentPixel++;
  125.             }
  126.             
  127.             currentLeftH += currentLeftIncrement;
  128.             currentRightH += currentRightIncrement;
  129.         }
  130.     }
  131.  
  132.     else if (vertex2V > vertex3V) {
  133.         long            currentLeftH,
  134.                         currentRightH,
  135.                         currentLeftIncrement,
  136.                         currentRightIncrement;
  137.         long            currentV; 
  138.         unsigned char    *currentPixel;
  139.                 
  140.         currentLeftH = (long)(vertex1H) << 16;
  141.         currentRightH = currentLeftH;
  142.  
  143.         currentLeftIncrement = ((long)(vertex2H - vertex1H) << 16) / (vertex2V - vertex1V);
  144.         currentRightIncrement = ((long)(vertex3H - vertex1H) << 16) / (vertex3V - vertex1V);
  145.             
  146.         for (currentV = vertex1V; currentV < vertex3V; currentV++) {
  147.             long    currentRightHlong = currentRightH >> 16;
  148.             long    currentLeftHlong = currentLeftH >> 16;
  149.             long    currentH;
  150.             
  151.             currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
  152.  
  153.             for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
  154.                 *currentPixel = color;
  155.                 currentPixel++;
  156.             }
  157.             
  158.             currentLeftH += currentLeftIncrement;
  159.             currentRightH += currentRightIncrement;
  160.         }
  161.  
  162.         currentRightH = (long)(vertex3H) << 16;
  163.         currentRightIncrement = ((long)(vertex2H - vertex3H) << 16) / (vertex2V - vertex3V);
  164.         
  165.         for (currentV = vertex3V; currentV <= vertex2V; currentV++) {
  166.             long    currentRightHlong = currentRightH >> 16;
  167.             long    currentLeftHlong = currentLeftH >> 16;
  168.             long    currentH;
  169.             
  170.             currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
  171.  
  172.             for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
  173.                 *currentPixel = color;
  174.                 currentPixel++;
  175.             }
  176.             
  177.             currentLeftH += currentLeftIncrement;
  178.             currentRightH += currentRightIncrement;
  179.         }
  180.     }
  181.     else {
  182.         long            currentLeftH,
  183.                         currentRightH,
  184.                         currentLeftIncrement,
  185.                         currentRightIncrement;
  186.         long            currentV; 
  187.         unsigned char    *currentPixel;
  188.                 
  189.         currentLeftH = (long)(vertex1H) << 16;
  190.         currentRightH = currentLeftH;
  191.  
  192.         currentLeftIncrement = ((long)(vertex2H - vertex1H) << 16) / (vertex2V - vertex1V);
  193.         currentRightIncrement = ((long)(vertex3H - vertex1H) << 16) / (vertex3V - vertex1V);
  194.             
  195.         for (currentV = vertex1V; currentV < vertex3V; currentV++) {
  196.             long    currentRightHlong = currentRightH >> 16;
  197.             long    currentLeftHlong = currentLeftH >> 16;
  198.             long    currentH;
  199.             
  200.             currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
  201.  
  202.             for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
  203.                 *currentPixel = color;
  204.                 currentPixel++;
  205.             }
  206.             
  207.             currentLeftH += currentLeftIncrement;
  208.             currentRightH += currentRightIncrement;
  209.         }
  210.     }
  211. }
  212.  
  213.  
  214.